package com.qingzhuge.verification.service.impl;

import com.qingzhuge.common.validator.ValidatorUtil;
import com.qingzhuge.service.BasePkLongServiceImpl;
import com.qingzhuge.verification.dao.SysAliCloudAuthDao;
import com.qingzhuge.verification.dto.CloudAuthRequest;
import com.qingzhuge.verification.dto.UserAuthRequest;
import com.qingzhuge.verification.dto.UserAuthResponse;
import com.qingzhuge.verification.entity.SysVerification;
import com.qingzhuge.verification.enums.EnumCloudType;
import com.qingzhuge.verification.service.IIdCardAuthCloudVerification;
import com.qingzhuge.verification.service.ISysVerificationService;
import com.qingzhuge.verification.utils.AliCloudVerification;
import com.qingzhuge.verification.utils.TencentcloudVerification;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.qingzhuge.verification.service.impl.SysVerificationImpl.CACHE_NAME;

/**
 * @author : zero.xiao
 * @description :
 * @date : 2018/9/29 0029 下午 16:08
 * @modified :
 * @since :
 */
@Service
@CacheConfig(cacheNames = CACHE_NAME)
public class SysVerificationImpl extends BasePkLongServiceImpl<SysAliCloudAuthDao, SysVerification> implements ISysVerificationService {
    static final String CACHE_NAME = "sysVerification";

    private IIdCardAuthCloudVerification getAliCloudVerification(EnumCloudType type){
        //从缓存中取数据
        SysVerification aliVerification = getCache("aliVerification");
        if (null == aliVerification) {
            aliVerification = baseDao.findByEnableIsTrueAndType(type);
            //更新缓存
            saveCache("aliVerification",aliVerification);
        }
        IIdCardAuthCloudVerification aliCloudVerification = null;
        if (null != aliVerification) {
            aliCloudVerification = AliCloudVerification.of(aliVerification);
        }
        return aliCloudVerification;
    }

    private IIdCardAuthCloudVerification getTencentCloudVerification(){
        //从缓存中取数据
        SysVerification tencentVerification = getCache("tencentVerification");
        if (null == tencentVerification) {
            tencentVerification = baseDao.findByEnableIsTrueAndType(EnumCloudType.tencentcloud);
            //更新缓存
            saveCache("tencentVerification",tencentVerification);
        }
        IIdCardAuthCloudVerification tencentCloudVerification = null;
        if (null != tencentVerification) {
            tencentCloudVerification = TencentcloudVerification.of(tencentVerification);
        }

        return tencentCloudVerification;
    }

    private IIdCardAuthCloudVerification getCloudVerification(EnumCloudType type){
        if (Objects.equals(EnumCloudType.aliyun,type)){
            return getAliCloudVerification(type);
        }else if(EnumCloudType.aliyunh5.equals(type)){
            return getAliCloudVerification(type);
        }else if(EnumCloudType.tencentcloud.equals(type)){
            return getTencentCloudVerification();
        }else{
            return null;
        }
    }

    @Override
    public UserAuthResponse doAuth(CloudAuthRequest authRequest) {
        log.debug("提交申请认证:{}", authRequest);
        ValidatorUtil.validateEntity(authRequest);
        UserAuthResponse userAuthResponse = null;
        IIdCardAuthCloudVerification verification = getCloudVerification(authRequest.getType());
        if(null != verification){
            userAuthResponse = verification.doAuth(authRequest);
        }
        return userAuthResponse;
    }

    @Override
    public UserAuthResponse getAuth(UserAuthRequest userAuthRequest) {
        log.debug("获取认证结果:{}", userAuthRequest);
        UserAuthResponse userAuthResponse = null;
        if (null != userAuthRequest) {
            ValidatorUtil.validateEntity(userAuthRequest);
            IIdCardAuthCloudVerification verification = getCloudVerification(userAuthRequest.getType());
            if(null != verification){
                userAuthResponse = verification.getAuth(userAuthRequest);
            }
        }
        return userAuthResponse;
    }
}
